﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace 无线打分网站
{
    [Serializable]
    public class UserExData
    {

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string NAMEField = "@Name";

        public const string EXPERIMENTIDField = "@ExperimentID";

        private Guid _ID;

        private String _Name;

        private String _ExperimentID;

        public Guid ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public String ExperimentID
        {
            get { return _ExperimentID; }
            set { _ExperimentID = value; }
        }
        public UserExData()
        {
            _ID = Guid.Empty;
            _Name = "";
            _ExperimentID = "";
        }
    }

    public class UserExDataFactory
    {
        public static UserExData Construct(IDataReader reader)
        {
            UserExData data = new UserExData();

            try { data.ID = (Guid)(reader["ID"]); }
            catch { }

            try { data.Name = Convert.ToString(reader["Name"]); }
            catch { }

            try { data.ExperimentID = Convert.ToString(reader["ExperimentID"]); }
            catch { }

            return data;
        }
        public static Guid GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Guid GetPrimaryKey(DataTable dt, int rowIndex)
        {
            Guid PrimaryKey = Guid.Empty;

            if (dt.Rows.Count < rowIndex + 1)

                return Guid.Empty;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return Guid.Empty;

            try { PrimaryKey = (Guid)(dr[UserExData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static UserExData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static UserExData Construct(DataTable dt, int rowIndex)
        {
            UserExData data = new UserExData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.Name = Convert.ToString(dr["Name"]); }
            catch { }

            try { data.ExperimentID = Convert.ToString(dr["ExperimentID"]); }
            catch { }

            return data;
        }
        public static UserExData Construct(DataRow dr)
        {
            UserExData data = new UserExData();

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.Name = Convert.ToString(dr["Name"]); }
            catch { }

            try { data.ExperimentID = Convert.ToString(dr["ExperimentID"]); }
            catch { }

            return data;
        }
        public static Guid GetPrimaryKey(DataRow dr)
        {
            Guid PrimaryKey = Guid.Empty;

            try { PrimaryKey = (Guid)(dr[UserExData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class UserExDataDBOption
    {
        public static int Insert(UserExData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into UserEx (");
            sql.Append("ID, ");
            sql.Append("Name, ");
            sql.Append("ExperimentID");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.ID.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Name.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.ExperimentID.ToString());
            sql.Append("'");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(UserExData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update UserEx set ");
            sql.Append("Name=");
            sql.Append("'");
            sql.Append(d.Name.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("ExperimentID=");
            sql.Append("'");
            sql.Append(d.ExperimentID.ToString());
            sql.Append("'");
            sql.Append(" where ID='");
            sql.Append(d.ID.ToString());
            sql.Append("'");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(UserExData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from UserEx ");
            sql.Append(" where ID='");
            sql.Append(d.ID.ToString());
            sql.Append("'");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from UserEx ");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataTable DataTableSelect(UserExData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from UserEx where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from UserEx ");
            return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
        }
        public static SqlDataReader ReaderSelect(UserExData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from UserEx ");
            return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
        }
        public static UserExData GetUserExData(Guid id)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from UserEx where ID='");
            sql.Append(id.ToString());
            sql.Append("'");
            DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
            if (dt.Rows.Count == 0)
                return null;
            else
            {
                UserExData ue = UserExDataFactory.Construct(dt.Rows[0]);
                return ue;
            }
        }
    }
}